const packageJson = require("./package.json");

// 引入
// const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const productionGZipExtensions = ["js", "css"]; //压缩的文件类型
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const path = require("path");
function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  publicPath: "./",
  productionSourceMap: false,
  outputDir: packageJson.name,

  devServer: {
    port: "4200",
    proxy: {
      "/api": {
        target: "http://123.56.230.79:6500",
        changeOrigin: true,
        pathRewrite: {
          "^/api": "",
        },
      },
    },
  },
  // 展示图形化信息
  chainWebpack: (config) => {
    // config.plugin("webpack-bundle-analyzer").use(BundleAnalyzerPlugin);
    // 移除prefetch插件，避免加载多余的资源
    config.plugins.delete("prefetch");
    // 移除 preload 插件，避免加载多余的资源
    config.plugins.delete("preload");
  },
  configureWebpack: (config) => {
    // 开启gzip压缩
    config.plugins.push(
      new CompressionWebpackPlugin({
        filename: (info) => {
          return `${info.path}.gz${info.query}`;
        },
        algorithm: "gzip",
        threshold: 10240, // 只有大小大于该值的资源会被处理 10K
        test: new RegExp("\\.(" + productionGZipExtensions.join("|") + ")$"),
        minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
        deleteOriginalAssets: false, // 删除原文件
      })
    );
    // webpack打包体积的性能提示
    config["performance"] = {
      maxEntrypointSize: 50000000,
      maxAssetSize: 30000000,
    };
    config.resolve.alias["@"] = resolve("src");
    config.resolve.alias["components"] = resolve("src/components");
    config.resolve.alias["api"] = resolve("src/api");
    config.resolve.alias["utils"] = resolve("src/components/utils");
    config.resolve.alias["plugins"] = resolve("src/components/plugins");
    config.resolve.alias["store"] = resolve("src/bid/store");
  },
};
